package com.zmh.service.http;


import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.zmh.common.DateUtil;
import com.zmh.dao.http.OamsgMapper;
import com.zmh.entity.MailEntity;
import com.zmh.entity.OamsgEntity;
import com.zmh.service.http.utils.HttpAPIService;
import com.zmh.service.msg.MailService;

/** 
 * 工作日志管理，提醒日志填写，自动批阅日志
 * @author: zhoumh
 * @version: 创建时间：2018年8月15日 下午5:13:23 
 * @Description: 
*/ 
@Service
public class OamsgHttp {
	private static Logger logger = LoggerFactory.getLogger(OamsgHttp.class);
	@Autowired
	private HttpAPIService httpAPIService;
	@Autowired
	private MailService mailService;
	@Autowired
	private OamsgMapper oamsgMapper;
	private String baseUrl = "http://60.213.47.147:8088/";
//	private String baseUrl = "http://192.168.8.251:8088/";
	/**
	 * 登陆IamtopNotes
	 * @author zhoumh
	 * @version 创建时间：2018年8月15日 下午5:13:23 
	 * @throws Exception 
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 */
	public boolean login() throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("authorization", "");
		map.put("login_username", "zmh");
		map.put("login_password", "8303523");
		map.put("bodyWidth", "1600");
		map.put("bodyHeight", "751");
		HttpResponse response = httpAPIService.doPostResp(baseUrl+"/seeyon/main.do?method=login", map);
		if ("ok".equals(response.getFirstHeader("LoginOK").getValue())) {
			logger.info("OA登录成功！");
			// 获取token
			return true;
		}
		return false;
	}
	/**
	 * 获取通知消息
	 * @return: void
	 * @author: zhoumh
	 * @version: 创建时间：2018年8月15日 下午5:13:23 
	 * @throws Exception 
	 * @Description:
	 */
	public void getOaMessge() throws Exception {
		if (!login()) {
			//如果登陆失败终止后续程序执行
			logger.error("登陆OA失败，本次运行终止。");
			return;
		}
		String result = httpAPIService.doGet(baseUrl+"/seeyon/message.do?method=showMessages&showType=0&readType=notRead");
		Document doc = Jsoup.parse(result);
		Elements es = doc.select("div#bDivmessageList > table#bTablemessageList > tbody#bodyIDmessageList > tr");
		String content = "收到OA消息： \n";
		for (Element e : es) {
			String type = e.child(0).text();
			String sendname = e.child(1).text();
			String sendtime = e.child(2).select("div").text();
			String title = e.child(3).select("a").text();
			String description = e.child(4).select("div > span").attr("id");
			OamsgEntity oamsg = oamsgMapper.getOneByDesc(description);
			if (oamsg == null) {
				oamsgMapper.insert(new OamsgEntity(sendname, sendtime, title, type, description));
				content = content.concat(type+" "+sendtime+" "+sendname+" \n"+title+"\n");
			}
		}
		if (content.equals("收到OA消息： \n")) {
			return;
		}
        mailService.saveMail(new MailEntity("OA消息<zhoumh1985@qq.com>", "zmh1812@qq.com", DateUtil.getDate()+"收到OA消息", content,"sendSimpleMail"));
	}
}
